﻿@model Tau.ViewModels.PageList

@{
    ViewBag.Title = "Manage Pages";
}

<h2>Manage Pages &nbsp; &nbsp; <a href="#" onclick="popupAddEdit();" style="cursor:pointer"> + Add New Page</a> &nbsp; | &nbsp;  <a href="#" class="addNew" onclick="popupAddParent();" style="cursor:pointer"> + Add Grouping</a></h2>



<div id="addParent" title="Add Grouping" style="display:none"></div>
<div id="addEdit" title="Add/Edit Page" style="display:none"></div>

<div id="pageList">
    @Html.Partial("ManagePages", Model)
</div>

<input type="button" value="save" onclick="savePagePositions();" />

<script type="text/javascript">
    function savePagePositions() {

        showProgress();

        //loop through each of the uls
        var parents = $(".droptrue");
        var index = 0;

        var parentIds = "";
        var childIds = "";

        parents.each(function () {

            var ulId = $(this).attr('id');
            parentIds = parentIds + ulId + "_";

            var children = $("#" + ulId).sortable('toArray');
            childIds = childIds + children.join('_') + '-';
        });

        //Update child page positions
        var urlAction = '@Url.Action("UpdatePagePositions", "PageAdmin")';
        urlAction = urlAction + "?parentIds=" + parentIds + "&pageIds=" + childIds;

        $.get(urlAction, function (data) {

            //Once all page positions are complete, update parent page order
            var parentOrders = $(":text");
            var orderParam = "";
            parentOrders.each(function () {

                var inputId = $(this).attr('id');
                var pageId = inputId.split("_")[1];
                orderParam = orderParam + pageId + "-" + $(this).attr('value') + "_";
            });

            //Update parent page order
            var urlAction = '@Url.Action("UpdateParentOrder", "PageAdmin")';
            urlAction = urlAction + "?parentOrder=" + orderParam;

            $.get(urlAction, function () {
                urlAction = '@Url.Action("ShowAllPages", "PageAdmin")';

                $("#pageList").load(urlAction, function () {
                    hideProgress();
                });
            });
        });
    }

    function deletePage(pageId, isParent) {

        var msg = "Are you sure you want to delete this page?";
        if (isParent == true) {
            msg = "Are you sure you want to delete this grouping?";
        }
        showYesNoDialog(
                        msg,

                        function () {
                            location.href = '@Url.Action("Delete", "PageAdmin")' + '?pageId=' + pageId;
                        });
    }

    function popupAddParent() {

        var url = '@Url.Action("AddParent", "PageAdmin")';

        $("#addParent").load(url, function () {

            $("#addParent").dialog({
                autoOpen: true,
                height: 140,
                width: 300,
                modal: true,
                close: function () {
                    location.href = '@Url.Action("Index", "PageAdmin")';
                }
            });
        });
    }

    function popupAddEdit(editId) {

        var url = '@Url.Action("AddEditPage", "PageAdmin")';

        if (editId != null && editId > 0) {

            $("#addEdit").load(url + "?pageId=" + editId, function () {

                $("#addEdit").dialog({
                    autoOpen: true,
                    height: 650,
                    width: 950,
                    modal: true,
                    close: function () {
                        location.href = '@Url.Action("Index", "PageAdmin")';
                    }
                });
            });
        }
        else {

            $("#addEdit").load(url, function () {

                $("#addEdit").dialog({
                    autoOpen: true,
                    height: 650,
                    width: 950,
                    modal: true,
                    close: function() {
                        location.href = '@Url.Action("Index", "PageAdmin")';
                    }
                });
            });
        }
    }
</script>
